<template>
  <a-modal
    title="新增收入支出配置"
    :width="900"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="handleCancel"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
                :labelCol="labelCol"
                :wrapperCol="wrapperCol"
                label="父级菜单"
                has-feedback
              >
               <a-select style="width: 100%" placeholder="請選擇一級收入" v-decorator="['pid', {rules: [{ required: true, message: '請選擇一級收入' }]}]" >
                <a-select-option v-for="(item,index) in menuTreeData" :key="index" :value="item.code">{{ item.name }}</a-select-option>
              </a-select>
        </a-form-item>
        <a-form-item
          label="名称"
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          has-feedback
        >
          <a-input placeholder="请输入名称" v-decorator="['name', {rules: [{required: true, message: '请输入名称！'}]}]" />
        </a-form-item>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
  import { busCostIncomeExpensesAdd, getYjcD } from '@/api/modular/main/buscostincomeexpenses/busCostIncomeExpensesManage'
  export default {
    data () {
      return {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 15 }
        },
        menuTreeData: [],
        visible: false,
        type: '',
        level: '',
        confirmLoading: false,
        form: this.$form.createForm(this)
      }
    },
    methods: {
      // 初始化方法
      add (type, level) {
        this.type = type
        this.level = level
        this.visible = true
        this.getYjcD()
      },
      getYjcD () {
         getYjcD({ 'type': this.type }).then((res) => {
          if (res.code === 200) {
            this.form.resetFields(`pid`, [])
            this.menuTreeData = res.data
          } else {
            this.$message.warning(res.msg)
          }
        })
      },
      /**
       * 提交表单
       */
      handleSubmit () {
        const { form: { validateFields } } = this
        this.confirmLoading = true
        validateFields((errors, values) => {
          if (!errors) {
            for (const key in values) {
              if (typeof (values[key]) === 'object') {
                values[key] = JSON.stringify(values[key])
              }
              values.type = this.type
              values.level = this.level
            }
            busCostIncomeExpensesAdd(values).then((res) => {
              if (res.code === 200) {
                this.$message.success('新增成功')
                this.confirmLoading = false
                this.$emit('ok', values)
                this.handleCancel()
              } else {
                this.$message.error(res.msg)// + res.message
              }
            }).finally((res) => {
              this.confirmLoading = false
            })
          } else {
            this.confirmLoading = false
          }
        })
      },
      handleCancel () {
        this.form.resetFields()
        this.visible = false
      }
    }
  }
</script>
